package com.hngy.educationaladministration.controller;

import com.hngy.educationaladministration.auth.Authority;
import com.hngy.educationaladministration.auth.Role;
import com.hngy.educationaladministration.config.CosConfig;
import com.hngy.educationaladministration.config.ParamContext;
import com.hngy.educationaladministration.entity.FileTable;
import com.hngy.educationaladministration.entity.Msg;
import com.hngy.educationaladministration.entity.Student;
import com.hngy.educationaladministration.service.FileTableService;
import com.hngy.educationaladministration.service.ProjectService;
import com.hngy.educationaladministration.service.StudentService;
import com.hngy.educationaladministration.service.TeacherService;
import com.hngy.educationaladministration.utils.DateUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.List;

/**
 * @Author: Deng JunHao
 * @Date: 2021-05-03
 * @Description:
 */
@Authority(roles = {Role.STUDENT, Role.TEACHER})
@Controller
@RequestMapping("/cos")
@Slf4j
public class FileController {
    @Autowired
    private FileTableService fileTableService;
    @Autowired
    private TeacherService teacherService;
    @Autowired
    private StudentService studentService;
    @Autowired
    private ProjectService projectService;
    //在线浏览文件后缀参数
    final static String LOOK_ONLINE = "?ci-process=doc-preview";

    @ResponseBody
    @PostMapping("/upload")
    public Msg upload(@RequestParam("file") MultipartFile inputFile, HttpServletRequest request) throws IOException {
        if (null == inputFile) {
            return Msg.fail().add("msg", "文件上传失败");
        }
        //获取student信息
        Student student = (Student) request.getSession().getAttribute("student");
        Long project_id = (Long) request.getSession().getAttribute("project_id");
        Long teacher_id = (Long) request.getSession().getAttribute("teacher_id");
        //id作为文件夹名
        Long studentId = student.getId();
        CosConfig.setName(studentId.toString());
        String filePath = CosConfig.uploadfile(inputFile);
        FileTable fileTable = new FileTable();
        fileTable.setSid(Math.toIntExact(studentId));
        fileTable.setTime(DateUtils.newTime());
        fileTable.setUrl(filePath);
        fileTable.setPid(Math.toIntExact(project_id));
        fileTable.setTid(Math.toIntExact(teacher_id));
        fileTableService.addUrl(fileTable);
        log.info("url：" + filePath);
        return Msg.success();
    }

    /**
     * get student upload files
     *
     * @param request
     * @param model
     * @return
     */
    @GetMapping("/getAllFiles")
    public String getAllFiles(HttpServletRequest request, Model model) {
        //获取student信息
        Student student = (Student) request.getSession().getAttribute("student");
        //id作为文件夹名
        Long id = student.getId();
        List<FileTable> allFileBySid = fileTableService.getAllFileBySid(id.intValue());
        allFileBySid.stream().forEach(fileTable -> getStudentName(fileTable));
        model.addAttribute("allFiles", allFileBySid);
        return "/student/myUpload";
    }

    public void getStudentName(FileTable fileTable) {
        fileTable.setStudentName(studentService.getStudentInfoBySid(fileTable.getSid().longValue()).getName());
        fileTable.setTeacherName(teacherService.getTeacherInfoByTid(fileTable.getTid().longValue()).getName());
        fileTable.setProjectName(projectService.getProjectInfoById(fileTable.getPid().longValue()).getProjectname());
    }

    @GetMapping("/delFile")
    public String delFile(Integer id) {
        String url = fileTableService.getInfoById(id).getUrl();
        CosConfig.delFile(url.replace(ParamContext.PIX, ""));
        fileTableService.delFileById(id);
        return "redirect:/student/getAllFiles";
    }
}